#include <QtSql>
#include <assert.h>
int main(int argc, char *argv[])
{
	QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
	db.setHostName("debian73");
	db.setDatabaseName("casual");
	db.setUserName("sa");
	/*
		要真的用 sa 這個帳號先去資料庫查看是否真的有資料
		不能使用su postgres進去查
	*/
	db.setPassword("buggy");
	qDebug() << "connecting";
	if (!db.open()) {
		qDebug() << db.lastError();
		return 0;
	} 
	qDebug() << "success";
	QSqlQuery query(db);
	assert(!query.isForwardOnly());
	bool ok = query.exec("select * from sp_login('elvis', '1234');");
	if (!ok)
		qDebug() << query.lastError() << "at " << QString::number(__LINE__);
	assert(query.size() > 0);
	assert(query.isActive());
	assert(ok == true);
	assert(query.isSelect() == true);
	assert(query.first() == true);
	assert(query.seek(0) == true);
	assert(query.isValid());
	QSqlRecord r = query.record();
	int count = r.count();
	qDebug() << "row count=" << query.size();
	qDebug() << "fields count=" << count;
	qDebug() << "fields name=" << r.fieldName(0);
	int index = r.indexOf("nickname");
	assert(index >= 0);
	QTextCodec* codec = QTextCodec::codecForName("utf8");
	QTextCodec::setCodecForLocale(codec);
	QTextCodec::setCodecForCStrings(codec);
	QString nickname = r.value("nickname").toString().toLocal8Bit();//store procedure must convert varchar(n) to binary(n) on linux version qt
	qDebug() << "nickname=" << nickname; 
	db.close();
	return 0;
}
